<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

/**
 * curl请求api
 * @author yuan
 */
class CurlApi {
    private $bank_loan_single_api = 'http://api.njhtjr.com/singleProgressQuery';//单独查询
    private $bank_loan_munch_api = 'http://api.njhtjr.com/batchProgressQuery';//批量查询
    private $sync_to_bank_api = 'http://api.njhtjr.com/pushDataToICBC';//批量查询
    private $sync_material_to_bank_api = 'http://api.njhtjr.com/uploadLoanDocument';//批量查询
    private $sync_material_query_api = "http://api.njhtjr.com/queryLoanMaterial";

    public function curl($url)
    {
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno'.curl_error($curl);
        }
        
        curl_close($curl); // 关键CURL会话
        return $tmpInfo; // 返回数据
    }
    
    /*
     * 银行贷款进度单个事项查询接口
     */
    public function curl_bank_loan_single($id = 0){
        $return_arr = array();
        if(isset($id) && !empty($id)){
            $result = $this->curl($this->bank_loan_single_api.'/'.$id);
            $result = json_decode($result,true);
            if(isset($result['code']) && 200==$result['code'] ){
                $return_arr = $result['data'];
            }
        }
        return $return_arr;
    }
    
    /*
     * 银行贷款进度批量查询接口
     */
    public function curl_bank_loan_batch($dept_id = 0 , $start_time = 0 , $end_time = 0 ,$begin_num = 1){
        $return_arr = array();
        if(!empty($dept_id) && !empty($start_time) && !empty($end_time) && !empty($begin_num)){
            $result = $this->curl($this->bank_loan_munch_api.'/'.$dept_id.'/'.$start_time.'/'.$end_time.'/'.$begin_num);
            $result = json_decode($result,true);
            if(isset($result['code']) && 200 == $result['code'] ){
                $return_arr = $result['data'];
            }
        }
        return $return_arr;
    }
    
    /*
     * 同步到银行
     */
    public function  curl_sync_to_bank($case_id)
    {
        $result = $this->curl($this->sync_to_bank_api.'/'.$case_id);
        return $result;
    }
    
    /*
     * 同步资料到银行
     */
    public function curl_material_sync_to_bank($document_id)
    {   
        $result = $this->curl($this->sync_material_to_bank_api.'/'.$document_id);
        return $result;
    }
    
    /*
     * 事项资料银行审核情况
     */
    public function curl_queryLoanMaterial($case_id)
    {   
        $result = $this->curl($this->sync_material_query_api.'/'.$case_id);
        return $result;
    }
}
